﻿<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Bezdelnick
 * Date: 12.04.13
 * Time: 15:13
 * To change this template use File | Settings | File Templates.
 */
require_once("Config.php");
require_once("common/Lang.php");
require_once("persistence/Country.php");
require_once("dao/CountryDao.php");

$countryDao = new CountryDao();
$countries = $countryDao->load();

if (isset($_POST['action']) && $_POST['action'] == "add") {
    if (isset($_POST['add_country_name'])) {
        $createdCountry = new Country();
        $createdCountry->setName($_POST['add_country_name']);
        $countryDao->insert($createdCountry);
    }
} else if (isset($_POST['action']) && $_POST['action'] == "edit") {
    if (isset($_POST['edit_country_id']) && isset($_POST['edit_country_name'])) {
        $editedCountry = new Country();
        $editedCountry->setId($_POST['edit_country_id']);
        $editedCountry->setName($_POST['edit_country_name']);
        $countryDao->update($editedCountry);
    }
} else if (isset($_POST['action']) && $_POST['action'] == "remove") {
    if (isset($_POST['remove_country_id'])) {
        $countryDao->delete($_POST['remove_country_id']);
    }
}
?>

<!-- Content table -->
<table class="content-table" border="0">
    <thead style="background-color: #ffd700;">
    <tr>
        <td style="width: 10%;">ID</td>
        <td style="width: 65%;">Name</td>
        <td style="width: 25%;">Operations</td>
    </tr>
    </thead>
    <tbody>
    <? foreach ($countries as $country) { ?>
    <tr style="background-color: #efefef;">
        <td><?= $country->getId() ?></td>
        <td><?= $country->getName() ?></td>
        <td style="text-align: center;">
            <div class="jquery-button edit-country-link"
                 itemId="<?= $country->getId() ?>"><?= Lang::EDIT ?>
            </div>
            <div class="jquery-button remove-country-link"
                 itemId="<?= $country->getId() ?>"><?= Lang::REMOVE ?>
            </div>
        </td>
    </tr>
        <? } ?>
    </tbody>
</table>